package cn.k.controller;

import cn.k.domain.Department;
import cn.k.qo.PageResult;
import cn.k.qo.QueryObject;
import cn.k.service.IDepartmentService;
import cn.k.util.RequirePermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    //处理分页查询页面请求
    @RequestMapping("/list")
    @RequirePermission(name = "部门查询",expression = "department:list")
    public String list(QueryObject qo, Model model){
        PageInfo<Department> pageInfo = departmentService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "department/list";
    }


    //处理删除请求
    @RequirePermission(name = "部门删除",expression = "department:delete")
    @RequestMapping("/delete")
    public String delete(Long id){
        if (id!=null){
            departmentService.delete(id);
        }
        return "redirect:/department/list";
    }


    //处理添加或者更新的请求
    @RequirePermission(name ="部门编辑",expression = "department:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Department department){
        //判断有没有id
        if (department.getId()==null){
            departmentService.insert(department);
        }else {
            departmentService.update(department);
        }
        return "redirect:/department/list";
    }


}
